var form = layui.form,id=getQueryVariable("id"),laydate = layui.laydate;

//表单初始值，修改时调用
if(id!=""){
    var oldData =  parent.table.cache["suppliertableid"];
    $.each(oldData, function (index, value) {
        if (oldData[index]["id"] == id) {
            form.val("newsupplier-form",{
                "customername":oldData[index]["customername"],
                "email":oldData[index]["email"],
                "cucustomerpost":oldData[index]["cucustomerpost"],
                "phone":oldData[index]["phone"],
                "mobilephone":oldData[index]["mobilephone"],
                "customerwechat":oldData[index]["customerwechat"],
                "expressaddress":oldData[index]["expressaddress"],
                "customerbirthday":oldData[index]["customerbirthday"],
                "purchaseman":oldData[index]["purchaseman"],
                "customerremark":oldData[index]["customerremark"],
            })
        }
    });
    form.render();
}
laydate.render({
    elem: '#customerbirthday'
});
//监听提交
form.on('submit(newsupplier-form-element)', function(data){
    //data.field 为表单修改或新增数据 变量ID有值的情况下是修改，否则为新增
    layer.msg("保存成功", {icon: 6});
    setTimeout(function () {
        var oldData =  parent.table.cache["suppliertableid"];
        if(id==""){
            data.field.id = oldData.length+1;
            oldData.push(data.field);
        }else {
            $.each(oldData, function (index, value) {
                if (oldData[index]["id"] == id) {
                    oldData[index]["customername"] = data.field.customername;
                    oldData[index]["email"] = data.field.email;
                    oldData[index]["cucustomerpost"] = data.field.cucustomerpost;
                    oldData[index]["phone"] = data.field.phone;
                    oldData[index]["mobilephone"] = data.field.mobilephone;
                    oldData[index]["customerwechat"] = data.field.customerwechat;
                    oldData[index]["expressaddress"] = data.field.expressaddress;
                    oldData[index]["customerbirthday"] = data.field.customerbirthday;
                    oldData[index]["purchaseman"] = data.field.purchaseman;
                    oldData[index]["customerremark"] = data.field.customerremark;
                }
            });
        }
        parent.table.reload('suppliertableid',{data : oldData}); //刷新联系人表格
        //保存成功关闭此页面
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);

    },1000)
    return false;
});
